import {RouterMount, createRouter} from 'uni-simple-router';
import { getStorageExpire } from './plugins/utils'

const router = createRouter({
	platform: process.env.VUE_APP_PLATFORM,
	routes: [
		...ROUTES,
		// 额外配置404，否则无法加载页面
		{
		  path: '*',
		  redirect:(to)=>{
			  return {name: '404'}
		  }
		}
	]
});

//全局路由前置守卫
// 监控路由，判断用户是否登录
router.beforeEach((to, from, next) => {
	const { meta: { auth }, path } = to,
	      access_token = getStorageExpire('access_token');
	if(auth && !access_token){
		uni.setStorageSync('to_path', path)
		next({
			name: 'login',
			NAVTYPE: "push"
		})
	}else{
		next()
	}
});

// 全局路由后置守卫
router.afterEach((to, from) => {
    console.log('跳转结束')
})

export {
	router,
	RouterMount
}